package com.jwsoft.manager.core.integration.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.data.core.util.PagerUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.vo.eduLotteryAnnouncementDetail.EduLotteryAnnouncementDetailVO;
import com.jwsoft.manager.core.dao.model.EduLotteryAnnouncementDetail;
import com.jwsoft.manager.core.dao.service.EduLotteryAnnouncementDetailService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import cn.hutool.core.convert.Convert;

import java.util.ArrayList;
import java.util.List;
import com.bifang.engine.assistant.core.exception.AppException;
import org.springframework.util.ObjectUtils;
import org.springframework.transaction.annotation.Transactional;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.jwsoft.manager.common.vo.eduLotteryAnnouncement.EduLotteryAnnouncementVO;
import com.jwsoft.manager.common.vo.eduLotteryAnnouncement.EduLotteryAnnouncementKeyVO;
import com.jwsoft.manager.common.vo.eduLotteryAnnouncement.EduLotteryAnnouncementQueryVO;
import com.jwsoft.manager.core.integration.EduLotteryAnnouncementIntegration;
import com.jwsoft.manager.core.dao.service.EduLotteryAnnouncementService;
import com.jwsoft.manager.core.dao.model.EduLotteryAnnouncement;
/**
 * 金华摇号结果公示业务实现类
 *
 * @author wangt
 * @since 2024-06-16
 */
@ApiService(funcCode = "eduLotteryAnnouncement", title = "金华摇号结果公示")
public class EduLotteryAnnouncementIntegrationImpl implements EduLotteryAnnouncementIntegration {

    @Autowired
    public EduLotteryAnnouncementService eduLotteryAnnouncementService;
    @Autowired
    private EduLotteryAnnouncementDetailService eduLotteryAnnouncementDetailService;

    @Override
    @OpApi(funcCode = "eduLotteryAnnouncement0001", title = "金华摇号结果公示分页查询", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE,publicFlag = BoolEnum.TRUE)
    public PageInfo<EduLotteryAnnouncementVO> getList(EduLotteryAnnouncementQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        LambdaQueryWrapper<EduLotteryAnnouncement> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtil.isNotBlank(vo.getSchoolId()),EduLotteryAnnouncement::getSchoolId,vo.getSchoolId())
                .eq(StringUtil.isNotBlank(vo.getAreaCode()),EduLotteryAnnouncement::getAreaCode,vo.getAreaCode())
                .like(StringUtil.isNotBlank(vo.getTitle()),EduLotteryAnnouncement::getTitle,vo.getTitle())
                .eq(StringUtil.isNotBlank(vo.getStatus()),EduLotteryAnnouncement::getStatus,vo.getStatus())
                .eq(vo.getYear()!=null,EduLotteryAnnouncement::getYear,vo.getYear())
                .orderByDesc(EduLotteryAnnouncement::getYear,EduLotteryAnnouncement::getCreateTime);
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());
        List<EduLotteryAnnouncement> list = eduLotteryAnnouncementService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduLotteryAnnouncement> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduLotteryAnnouncementVO> resultList = Convert.toList(EduLotteryAnnouncementVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "eduLotteryAnnouncement0002", title = "金华摇号结果公示根据主键查询详情", funcType = FuncTypeEnum.query,checkPrivilege = BoolEnum.FALSE,publicFlag = BoolEnum.TRUE)
    public EduLotteryAnnouncementVO getById(EduLotteryAnnouncementKeyVO vo) {
        EduLotteryAnnouncement entity = eduLotteryAnnouncementService.getById(vo.getLotteryAnnouncementId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduLotteryAnnouncementVO result = Convert.convert(EduLotteryAnnouncementVO.class, entity);
        LambdaQueryWrapper<EduLotteryAnnouncementDetail> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(EduLotteryAnnouncementDetail::getLotteryAnnouncementId,vo.getLotteryAnnouncementId())
                .orderByAsc(EduLotteryAnnouncementDetail::getSerialNum);
        List<EduLotteryAnnouncementDetail> list = eduLotteryAnnouncementDetailService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)){
            result.setList(new ArrayList<>());
        }else{
            result.setList(Convert.toList(EduLotteryAnnouncementDetailVO.class, list));
        }
        return result;
    }
    /** 保存前统一校验*/
    @Override
    @OpApi(funcCode = "eduLotteryAnnouncement0003", title = "金华摇号结果公示保存前校验", funcType = FuncTypeEnum.other ,checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduLotteryAnnouncementVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "eduLotteryAnnouncement0004", title = "金华摇号结果公示新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "eduLotteryAnnouncement0004",
            operationName = "金华摇号结果公示新增",
            dataType = "lotteryAnnouncementId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduLotteryAnnouncementVO add(EduLotteryAnnouncementVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        EduLotteryAnnouncement entity = Convert.convert(EduLotteryAnnouncement.class, vo);
        eduLotteryAnnouncementService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setLotteryAnnouncementId(entity.getLotteryAnnouncementId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduLotteryAnnouncement0005", title = "金华摇号结果公示修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "eduLotteryAnnouncement0005",
            operationName = "金华摇号结果公示修改",
            dataType = "lotteryAnnouncementId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduLotteryAnnouncementVO edit(EduLotteryAnnouncementVO vo) {
        if (ObjectUtils.isEmpty(vo.getLotteryAnnouncementId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduLotteryAnnouncement oldEntity = eduLotteryAnnouncementService.getById(vo.getLotteryAnnouncementId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduLotteryAnnouncement entity = Convert.convert(EduLotteryAnnouncement.class, vo);
        eduLotteryAnnouncementService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "eduLotteryAnnouncement0006", title = "金华摇号结果公示根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "eduLotteryAnnouncement0006",
            operationName = "金华摇号结果公示删除",
            dataType = "lotteryAnnouncementId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduLotteryAnnouncementKeyVO vo) {
        if (ObjectUtils.isEmpty(vo.getLotteryAnnouncementId())) {
            throw new AppException("传入需要删除的数据主键不能为空!");
        }
        EduLotteryAnnouncement entity = eduLotteryAnnouncementService.getById(vo.getLotteryAnnouncementId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("未找到需要删除的信息!");
        }
        eduLotteryAnnouncementService.removeById(vo.getLotteryAnnouncementId());
    }
}
